package com.app.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.app.model.Menu;
import com.app.service.MenuService;


@Controller
@RequestMapping("/web/menu")
public class MenuController {
	@Resource
	private MenuService menuService;
	@Resource
	private MenuQuery menuQuery;
	
	@RequestMapping(value = "")
	public String index() {
		return "auth/menu";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Object getList(HttpServletRequest request) {
		/*Map data = new HashMap();
		List<Menu> list = menuService.findAll();
		data.put("data", list);
		return data;*/
		return menuQuery.queryPage(menuQuery.getParams(request));
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Object save(Menu menu) {
		String msg = null;
		try {
			menuService.save(menu);
		} catch (Exception e) {
			msg = "新增权限出错";
		}
		return msg;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(Menu menu) {
		String msg = null;
		try {
			menuService.update(menu);
		} catch (Exception e) {
			msg = "更新权限出错";
		}
		return msg;
	}
	
	@RequestMapping("/getById")
	@ResponseBody
	public Object getById(Long id) {
		return menuService.getById(id);
	}
	
	@RequestMapping("/getParentMenu")
	@ResponseBody
	public Object getParentMenu() {
		return menuService.getParentMenu();
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		String msg = null;
		try {
			menuService.delete(id);
		} catch (Exception e) {
			msg = "删除权限出错";
		}
		return msg;
	}
}
